package com.mypage.view.activity.beautMore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mypage.adapter.NewConvertAdapter;
import com.mypage.bean.ConvertState;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConvertPupopActivity extends Activity {


    @Bind(R.id.changelistView)
    ListView mListView;
    @Bind(R.id.imgDismiss)
    ImageView imgDismiss;
    @Bind(R.id.imgYes)
    ImageView imgYes;
    @Bind(R.id.btnNo)
    RelativeLayout btnNo;
    private List<ConvertState.State> mList;
    private NewConvertAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convert_pupop);
        ButterKnife.bind(this);
        initData();
        initLister();
    }
    private String state;
    private void initData() {
        state = getIntent().getStringExtra("state");
        stateData();
    }

    public void initLister() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                imgYes.setVisibility(View.INVISIBLE);
                setCancelChe();
                setSelectChe(i);
                mAdapter.notifyDataSetChanged();
                Intent intent = new Intent();
                intent.putExtra("mName", mList.get(i).codevalue);
                setResult(2, intent);
                finish();
            }
        });
        if (state.contains("无")||state.contains("No")){
            imgYes.setVisibility(View.VISIBLE);
        }else{
            imgYes.setVisibility(View.INVISIBLE);
        }
    }

    public void setCancelChe() {
        for (int i = 0; i < mList.size(); i++) {
            if (NewConvertAdapter.getChck(i)) {
                NewConvertAdapter.setChck(i, false);
            }
        }

    }

    public void setSelectChe(int i) {
        NewConvertAdapter.setChck(i, true);
    }

    @OnClick({R.id.imgDismiss, R.id.btnNo})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgDismiss:
                finish();
                break;
            case R.id.btnNo:
                imgYes.setVisibility(View.VISIBLE);
                Intent intent = new Intent();
                intent.putExtra("mName", getString(R.string.no_no));
                setResult(2, intent);
                finish();
                break;
        }
    }

    public void stateData() {
        HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
        params.addBodyParameter("serviceName", "getLeadConvertCode");


        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
//                parseData(arg0.result.toString());
                String s = arg0.result.toString();
                ConvertState convert = new Gson().fromJson(arg0.result.toString(), ConvertState.class);
                mList = convert.data;
                mAdapter = new NewConvertAdapter(ConvertPupopActivity.this, mList,state);
                mListView.setAdapter(mAdapter);
            }


        });
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        RunTimeManager.getInstance().setIsrem(false);
//        finish();
//        return super.onTouchEvent(event);
//    }


}
